isOpenJar();

    function isOpenJar(){
        var request = "123123";

        notify_CORS(request,function(data){
            console.log("response==="+data);
            if( data == "ok"){
                console.log("local server is ONLINE");
                start();
            }
            else{
                console.log("local server is not OFF_LINE");
            }
        });

    }

    function start(){
        var vs = document.getElementsByClassName("video_list video_clearfix");

        if( vs.length <= 0 ){
            console.log("没有找到 video_list video_clearfix");
            return;
        }
        console.log("找到 video_list video_clearfix");
        var vilist = vs[0];
        var as = vilist.getElementsByTagName("a");
        if ( as.length <=0 ){
            console.log("没有找到 a");
            return ;
        }
        var index = 0;
        //获取到每个页面
        console.log("获取到每个页面")
        for( var i = 0;i < as.length;i++){
            var pageUrl = as[i].href;
            var pageTitle = as[i].innerText.replace(/\s+/g,"");
            if ( pageTitle.indexOf("直播课堂") ==-1 ){
                //console.log(pageTitle+"__"+pageUrl);
                //let m3u8Url = getM3u8FromUrl(pageUrl);
                //let perInfo = {"index":index++,"title":pageTitle,"url":m3u8Url};
                //result.push(perInfo);
                //
                //console.log("pageTitle=="+pageTitle);
                analyPerHtml(pageUrl,pageTitle,index++);
            }
        }
    }
    //解析每个页面，获取m3u8的地址
    function analyPerHtml(pageUrl,title,index){
        //console.log("pageUrl=="+pageUrl);
        var response = GET(pageUrl);
        //console.log("response=="+response);
        //GET_Syn(pageUrl,function (response) {
            var patten = /aliyun.yijincc.com[\s\S]*?m3u8[\s\S]*?'>/;
            var m3u8Urls = patten.exec(response);
            var m3U8Url = "";
            if(m3u8Urls==undefined || m3u8Urls.length <= 0){
                console.log("没有找到"+pageUrl);
                return;
            }
            else{
                m3U8Url = m3u8Urls[0];
                m3U8Url = "https://"+m3U8Url.substring(0,m3U8Url.length-2);
               // console.log("找到了"+m3U8Url);
                //找到了m3u8地址后，开始下载m3u8文件，判断是否需要解密，并且获取解密key

                getKey(m3U8Url,title,index);

            }
        //});
    }
    function notify(data){
        notify_CORS(data,null);
    }
    function NOT_USE_n_otify(data){
        var url = "http:127.0.0.1:12345/test.php";

        console.log("sendData==="+data);
        var httpRequest = new XMLHttpRequest();//第一步：建立所需的对象
        httpRequest.open('POST', url, true);//第二步：打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"
        httpRequest.setRequestHeader('access-control-allow-headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , Access-Control-Allow-Origin, Referer')
        httpRequest.setRequestHeader('Access-Control-Allow-Origin', '*')
        httpRequest.setRequestHeader('Access-Control-Allow-Methods', 'POST')
       // httpRequest.setRequestHeader("Accept-Language","zh");
       // httpRequest.setRequestHeader("Accept-Encoding","gzip");
        console.log("window.location.pathname=="+window.location.href);
       // httpRequest.setRequestHeader("Referer",window.location.href);
        httpRequest.send(data);//第三步：发送请求  将请求参数写在URL中

        var response = httpRequest.responseText;

        httpRequest.onreadystatechange = function () {
            if ( httpRequest.readyState === 4 && ( httpRequest.status>=200 && httpRequest.status<300 )  ){
                var response = httpRequest.responseText;

            }
        };
    }

     function notify_CORS(data,callback){
        var url = "http:127.0.0.1:12345/test.php";
        function createCORS(method, url){
            var xhr = new XMLHttpRequest();
            if('withCredentials' in xhr){
                xhr.open(method, url, true);
            }else if(typeof XDomainRequest != 'undefined'){
                xhr = new XDomainRequest();
                xhr.open(method, url);
            }else{
                xhr = null;
            }
            return xhr;
        }

        var request = createCORS('post',url);
        if(request){
            request.onload = function(response){
                console.log("get response=="+request.response);
                if( !!callback ){
                    callback(request.response);
                }
            };
            request.send(data);
       }
    }
    //根据m3u8文件获取key数据
    function getKey(m3u8_url,title,index){
        var name = index+"_"+title;
        var response = GET(m3u8_url);
        //GET_Syn(m3u8_url,function (response) {
            //解析结果
            var pattern = /https[\s\S]*?Ciphertext[\s\S]*?\"/;
            var keyUrls = pattern.exec(response);
            if(keyUrls==undefined || keyUrls.length <= 0){
                //直接通知下载
                var js = {"url":m3u8_url,"title":name,"key":"null"};
                console.log(JSON.stringify(js));
                notify(JSON.stringify(js));
            }
            else{
                //没有key，没有加密的视频
                var keyUrl = keyUrls[0];
                //key地址
                keyUrl = keyUrl.substring(0,keyUrl.length-1);
                //   alert("data"+keyUrl);
                getKeyUrlByData(keyUrl,m3u8_url,name);
            }
       //});
    }

    function getKeyUrlByData(url,m3u8_url,name){
        var httpRequest = new XMLHttpRequest();//第一步：建立所需的对象
        httpRequest.open('GET', url, true);//第二步：打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"
        //httpRequest.setRequestHeader("Accept-Language","zh");
        //httpRequest.setRequestHeader("Accept-Encoding","gzip");
        //httpRequest.setRequestHeader("Origin","https://ke.yijincc.com");
        //httpRequest.setRequestHeader("Accept","*/*");
        // httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.responseType = "arraybuffer";
        httpRequest.onload = function (oEvent) {
            var arrayBuffer = httpRequest.response; // 注意:不是oReq.responseText
            if (arrayBuffer) {
                var b64 = strToHex(arrayBuffer);
                console.log("b64=="+b64);
                if( b64.length == 32 ){
                    var js = {"url":m3u8_url,"title":name,"key":b64};
                    //alert(JSON.stringify(js));
                    notify(JSON.stringify(js));
                    return;
                }
                else{
                    console.log("获取失败");
                    return;
                }
            }
        };
        httpRequest.send(null);//第三步：发送请求  将请求参数写在URL中
    }
    function GET(url ){
        var httpRequest = new XMLHttpRequest();//第一步：建立所需的对象
        httpRequest.open('GET', url, false);//第二步：打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"
        httpRequest.setRequestHeader("Accept-Language","zh");
        httpRequest.setRequestHeader("Accept","*/*");
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.send();//第三步：发送请求  将请求参数写在URL中
        return httpRequest.response;
    }
    function GET_Syn(url,callback ){
        var httpRequest = new XMLHttpRequest();//第一步：建立所需的对象
        httpRequest.open('GET', url, true);//第二步：打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"
        httpRequest.setRequestHeader("Accept-Language","zh");
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.send();//第三步：发送请求  将请求参数写在URL中
        httpRequest.onreadystatechange = function () {
            if ( httpRequest.readyState === 4 && ( httpRequest.status>=200 && httpRequest.status<300 )  ){
                var response = httpRequest.responseText;
                callback(response);
            }
        };
    }
    function strToHex(arrayBuffer){

        let res = "";
        var byteArray = new Uint8Array(arrayBuffer);
        for (var i = 0; i < byteArray.byteLength; i++) {
            // 对数组中的每个字节进行操作
            let code = byteArray[i];
            //console.log("index="+i+"__code=="+code);
            let tmp = ((code).toString(16));
            if (tmp.length==1){
                res +=( "0"+tmp);
            }
            else{
                res += tmp;
            }
        }
        return res;
    }